Branchwell

Lumen & Gesso / website · lumengesso.com / app / api / studio / print / asset / [id] / route.ts

1import { paidPrintAsset } from '../../persistence';
2import { PRINT_HEADERS, printAssetRequestMatches, validPrintOrder } from '../../protocol';
3
4export const runtime = 'nodejs';
5export const dynamic = 'force-dynamic';
6
7export async function GET(req: Request, ctx: { params: Promise<{ id: string }> }) {
8 const { id } = await ctx.params;
9 const invalid = () => new Response(null, { status: 404, headers: PRINT_HEADERS });
10 if (!validPrintOrder(id) || !printAssetRequestMatches(req.url, id)) return invalid();
11 try {
12 const asset = await paidPrintAsset(id, req.url);
13 return asset
14 ? new Response(new Uint8Array(asset.bytes), { headers: { ...PRINT_HEADERS, 'Content-Type': asset.mime } })
15 : invalid();
16 } catch {
17 return new Response(null, { status: 503, headers: PRINT_HEADERS });
18 }
19}